package com.example.home;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.databinding.HomePreonlineCardBinding;
import com.example.home.viewmodel.HomePreonlineViewModel;
import com.example.home.viewmodel.HomeViewModel;

import org.jetbrains.annotations.NotNull;

public class PreonlineRAdapter extends RecyclerView.Adapter<PreonlineRAdapter.PreonlineRHolder> {

    private static final String TAG = "PreonlineRAdapter";
    private HomePreonlineViewModel viewModel;
    public IStartGameDetailActivity iStartGameDetailActivity;//点击跳转接口

    //传递ViewModel
    public PreonlineRAdapter(HomePreonlineViewModel homePreonlineViewModel){
        viewModel=homePreonlineViewModel;
    }

    @Override
    public PreonlineRHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        HomePreonlineCardBinding binding= DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                R.layout.home_preonline_card,parent,false);
        PreonlineRHolder holder= new PreonlineRHolder(binding);//创建item并绑定binding
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull PreonlineRAdapter.PreonlineRHolder holder, int position) {
        holder.binding.setData(viewModel.getGames().get(position));//数据绑定
        holder.binding.homeRecommendCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //fragment中将接口实例化
                iStartGameDetailActivity.openById(holder.binding.getData().getGame().getId());
            }
        });
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < (Math.min(holder.binding.getData().getCategory().size(), 3)); i++) {
            if (holder.binding.getData().getCategory().get(i).getName().equals("TapTap独家")) {
//                str.append(holder.binding.getData().getCategories().size() < 4 ? "" : holder.binding.getData().getCategories().get(3).getName());
                str.append("BatBat独家！！！");
                str.append("  ");
            } else {
                str.append(holder.binding.getData().getCategory().get(i).getName());
                str.append("  ");
            }
        }
        holder.binding.homePreonlineCardGameCategories.setText(str);
    }

    @Override
    public int getItemCount() {
        //空值返回0
        if(viewModel.getGames()==null){
            return 0;
        }else {
            return viewModel.getGames().size();
        }
    }

    /***************************************************************************************/
    static class PreonlineRHolder extends RecyclerView.ViewHolder{
        private HomePreonlineCardBinding binding;

        public PreonlineRHolder(HomePreonlineCardBinding binding) {
            super(binding.getRoot());
            this.binding=binding;
        }
    }
}

